package com.itheima.tlias.controller;


import com.itheima.tlias.pojo.Emp;
import com.itheima.tlias.pojo.EmpQueryParam;
import com.itheima.tlias.pojo.PageResult;
import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
	@Autowired
	private EmpService empService;

	@GetMapping
	public Result page(EmpQueryParam eqp) {
		log.info("分页查询，参数：name={}, gender={}, begin={}, end={}, page={}, pageSize={}", eqp.getName(), eqp.getGender(), eqp.getBegin(), eqp.getEnd(), eqp.getPage(), eqp.getPageSize());
		PageResult<Emp> pageResult = empService.page(eqp.getName(), eqp.getGender(), eqp.getBegin(), eqp.getEnd(), eqp.getPage(), eqp.getPageSize());
		return Result.success(pageResult);
	}

	@PostMapping
	public Result save(@RequestBody Emp emp) throws Exception {
		log.info("保存员工，数据：{}", emp);
		//try {
		//	empService.save(emp);
		//} catch (Exception e) {
		//	return Result.error("保存失败:" + e.getMessage());
		//}
		empService.save(emp);
		return Result.success();
	}

	@GetMapping("/list")
	public Result findAll() {
		List<Emp> list = empService.listAll();
		return Result.success(list);
	}


	//@DeleteMapping
	//public Result deleteByIds(Integer[] ids) {
	//	log.info("批量删除员工，参数：{}", Arrays.toString(ids));
	//	//empService.deleteByIds(ids);
	//	return Result.success();
	//}

	@DeleteMapping
	public Result deleteByIds(@RequestParam List<Integer> ids) {
		log.info("批量删除员工，参数：{}", ids);
		empService.deleteByIds(ids);
		return Result.success();
	}

	@GetMapping("/{id}")
	public Result infoById(@PathVariable("id") Integer id) {
		Emp emp = empService.infoById(id);
		//long threadId = Thread.currentThread().getId();
		//log.info("在Controller中的线程id为：{}", threadId);
		return Result.success(emp);
	}

	@PutMapping
	public Result update(@RequestBody Emp emp) {
		log.info("更新员工，参数：{}", emp);
		empService.update(emp);
		return Result.success();
	}
}
